package validator

import (
	"fmt"
	"time"

	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay/models"
)

// ValidateCardHolderRequest 验证持卡人创建请求
func ValidateCardHolderRequest(req *models.CardHolderRequest) error {
	if req.FirstName == "" {
		return fmt.Errorf("first_name is required")
	}
	if req.LastName == "" {
		return fmt.Errorf("last_name is required")
	}
	if req.Email == "" {
		return fmt.Errorf("email is required")
	}
	if req.MobilePrefix == "" {
		return fmt.Errorf("mobile_prefix is required")
	}
	if req.Mobile == "" {
		return fmt.Errorf("mobile is required")
	}
	if req.BirthDate == "" {
		return fmt.Errorf("birth_date is required")
	}
	// 验证出生日期格式
	if _, err := time.Parse("2006-01-02", req.BirthDate); err != nil {
		return fmt.Errorf("invalid birth_date format, should be YYYY-MM-DD")
	}
	if req.BillingCity == "" {
		return fmt.Errorf("billing_city is required")
	}
	if req.BillingAddress == "" {
		return fmt.Errorf("billing_address is required")
	}
	if req.BillingZipCode == "" {
		return fmt.Errorf("billing_zip_code is required")
	}
	if req.ChannelID <= 0 {
		return fmt.Errorf("channel_id is required")
	}
	if req.CountryCode <= 0 {
		return fmt.Errorf("country_code is required")
	}
	if req.BillingState == "" {
		return fmt.Errorf("billing_state is required")
	}
	return nil
}

// ValidateCardApplyRequest 验证开卡申请请求
func ValidateCardApplyRequest(req *models.CardApplyRequest) error {
	if req.ChannelType != 1 && req.ChannelType != 2 {
		return fmt.Errorf("invalid channel_type, should be 1 or 2")
	}
	if req.BinRangeID == "" {
		return fmt.Errorf("bin_range_id is required")
	}
	if req.CardAlias == "" {
		return fmt.Errorf("card_alias is required")
	}
	if req.ChannelType == 2 && req.GroupID <= 0 {
		return fmt.Errorf("group_id is required for shared card")
	}
	if req.AuthLimitAmount <= 0 {
		return fmt.Errorf("auth_limit_amount must be greater than 0")
	}
	if req.CardUserID <= 0 {
		return fmt.Errorf("card_user_id is required")
	}
	// 验证开始日期格式
	if req.StartDate == "" {
		return fmt.Errorf("start_date is required")
	}
	if _, err := time.Parse("2006-01-02", req.StartDate); err != nil {
		return fmt.Errorf("invalid start_date format, should be YYYY-MM-DD")
	}
	// 验证结束日期格式
	if req.EndDate == "" {
		return fmt.Errorf("end_date is required")
	}
	if _, err := time.Parse("2006-01-02", req.EndDate); err != nil {
		return fmt.Errorf("invalid end_date format, should be YYYY-MM-DD")
	}
	// 验证结束日期必须大于开始日期
	startDate, _ := time.Parse("2006-01-02", req.StartDate)
	endDate, _ := time.Parse("2006-01-02", req.EndDate)
	if endDate.Before(startDate) {
		return fmt.Errorf("end_date must be after start_date")
	}
	return nil
}

// ValidateCardRechargeRequest 验证充值请求
func ValidateCardRechargeRequest(req *models.CardRechargeRequest) error {
	if req.CardID <= 0 {
		return fmt.Errorf("card_id is required")
	}
	if req.Amount <= 0 {
		return fmt.Errorf("amount must be greater than 0")
	}
	return nil
}
